import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'font-awesome/css/font-awesome.min.css'

import {postKeyValueRequest} from "./utils/api";
import {postRequest} from "./utils/api";
import {getRequest} from "./utils/api";
import {putRequest} from "./utils/api";
import {deleteRequest} from "./utils/api";
import {initMenu} from "./utils/menus";

// 通过将请求接口注入到 Vue 中，可以减少在各个组件的导入操作，直接通过 this 调用
Vue.prototype.postKeyValueRequest = postKeyValueRequest;
Vue.prototype.postRequest = postRequest;
Vue.prototype.getRequest = getRequest;
Vue.prototype.putRequest = putRequest;
Vue.prototype.deleteRequest = deleteRequest;

Vue.config.productionTip = false
Vue.use(ElementUI);

// 配置全局路由守卫
router.beforeEach((to,from,next) =>{
  // 如果跳转的页面是登录页面，则直接放行
  if (to.path == '/'){
    next();
  }else {
    // 用户已经登录
    if (window.sessionStorage.getItem("user")){
      //渲染路由菜单
      initMenu(router,store);
      next();
    } else{
      // 将要跳转的路径作为参数
      next('/?redirect=' + to.path);
    }
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
